﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Linq;
using MyRssReader.Temp.RSS;

namespace MyRssReader.Temp
{
    class Class1
    {
        static void Main2( string[] args )
        {
            // Create a request to the url
            String sRss = "http://ostrie.moskva.com/rss/";
            HttpWebRequest request = HttpWebRequest.Create( sRss ) as HttpWebRequest;
            request.UseDefaultCredentials = true;

            HttpWebResponse response = null;
            try { response = request.GetResponse() as HttpWebResponse; }
            catch( WebException ) { /*return null; */}

            // Regular expression for an HTML title
            string regex = @"(?<=<title.*>)([\s\S]*)(?=</title>)";


            XmlDocument xmlDocument = new XmlDocument();
            // If the correct HTML header exists for HTML text, continue
            if( new List<string>( response.Headers.AllKeys ).Contains( "Content-Type" ) )
            {
                //HTML 
                if( response.Headers[ "Content-Type" ].StartsWith( "text/html" ) )
                {
                    // Download the page
                    WebClient web = new WebClient();
                    web.Encoding = Encoding.UTF8;

                    web.UseDefaultCredentials = true;

                    string page = web.DownloadString( sRss );
                    Regex ex = new Regex( regex, RegexOptions.IgnoreCase );
                    string result = ex.Match( page ).Value.Trim();
                }
                //XML (may be rss)
                if( response.Headers[ "Content-Type" ].StartsWith( "text/xml" ) )
                {
                    // Download the page
                    WebClient web = new WebClient();
                    web.Encoding = Encoding.GetEncoding( "koi8-r" );

                    web.UseDefaultCredentials = true;

                    string page = web.DownloadString( sRss );

                    xmlDocument.LoadXml( page );
                }
            }

            //XElement elRoot = new XElement( xmlDocument.DocumentElement.Name, xmlDocument.DocumentElement.InnerXml );
            XElement elRoot = XElement.Parse( xmlDocument.DocumentElement.InnerXml );
            XDocument xDoc = new XDocument( elRoot );
            List<XElement> items = elRoot.Elements( "item" ).ToList();

            RssFeed rssFeed = new RssFeed();


            rssFeed.Title = elRoot.Element( "title" ).Value;
            rssFeed.Description = elRoot.Element( "description" ).Value;
            rssFeed.Link = elRoot.Element( "link" ).Value;



            foreach( XElement element in items )
            {
                rssFeed.Items.Add( new RssItem()
                {
                    Description = element.Element( "description" ).Value
                   ,
                    Link = element.Element( "link" ).Value
                   ,
                    Title = element.Element( "title" ).Value
                   ,
                    PubDate = DateTime.Parse( element.Element( "pubDate" ).Value )
                } );
            }

            int f = 3;

        }

    }
}
